package cn.edu.neusoft.neufooddemo.activity;

import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

import cn.edu.neusoft.neufooddemo.R;
import cn.edu.neusoft.neufooddemo.adapter.FoodListAdapter;
import cn.edu.neusoft.neufooddemo.bean.CollectStatus;
import cn.edu.neusoft.neufooddemo.bean.FoodInfo;
import cn.edu.neusoft.neufooddemo.bean.Result;
import cn.edu.neusoft.neufooddemo.common.Constants;
import cn.edu.neusoft.neufooddemo.listener.RetrofitListener;
import cn.edu.neusoft.neufooddemo.model.FoodModel;
import cn.edu.neusoft.neufooddemo.model.UserModel;

public class FoodListActivity extends BaseActivity implements RetrofitListener{
    private RecyclerView recyclerView;
    private String Shop_id,result;
    private TextView foodlist_shopname;
    private ImageView foodlist_return, foodlist_collect;
    private FoodListAdapter adapter;
    private FoodModel foodModel;
    private UserModel userModel;
    private String phonenum;

     protected void initViews(){

         setLayout(R.layout.activity_foodlist);
        recyclerView = (RecyclerView) findViewById(R.id.foodlist);
        recyclerView.setLayoutManager(new LinearLayoutManager(FoodListActivity.this));
        foodlist_shopname = (TextView) findViewById(R.id.foodlist_shopname);
        foodlist_return = (ImageView) findViewById(R.id.foodlist_return);
        foodlist_collect = (ImageView) findViewById(R.id.foodlist_collect);
    }

     void initEvents(){
        foodlist_return.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        foodlist_collect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                collect();
            }
        });
    }

     void initData(){
        Intent intent = getIntent();
        Shop_id = intent.getStringExtra("id");
        phonenum=intent.getStringExtra("phonenum");
        foodlist_shopname.setText(intent.getStringExtra("shopname"));
        getFoodList();
        iscollected();
    }

    private void getFoodList()
    {
        foodModel=new FoodModel();
        foodModel.getFoodByShop(Shop_id,this);
    }

    private void iscollected(){
        foodModel=new FoodModel();
        foodModel.isCollected(getUser_id(), Shop_id,"0",this);
    }

    private void collect(){
        userModel = new UserModel();
        userModel.userCollectShop(getUser_id(), Shop_id,this);
    }

    @Override
    public void onSuccess(Object o, int flag) {
        switch (flag)
        {
            case Constants.GETFOODBYSHOP:
                adapter = new FoodListAdapter(FoodListActivity.this, (List<FoodInfo>)o, R.layout.cardview_food,phonenum);
                recyclerView.setAdapter(adapter);
                break;
            case Constants.ISCOLLECTED:
                result=((CollectStatus)o).getCollected().toString();
                if (result.equals("0")) {
                    foodlist_collect.setImageResource(R.drawable.uncollection);
                } else {
                    foodlist_collect.setImageResource(R.drawable.collection);
                }
                break;
            case Constants.USERCOLLECTSHOP:
                if (((Result)o).getSuccess().equals("1")) {
                    if (result.equals("0")) {
                        foodlist_collect.setImageResource(R.drawable.collection);
                        Toast.makeText(FoodListActivity.this, "收藏成功", Toast.LENGTH_SHORT).show();
                        result = "1";
                    } else {
                        foodlist_collect.setImageResource(R.drawable.uncollection);
                        Toast.makeText(FoodListActivity.this, "取消收藏成功", Toast.LENGTH_SHORT).show();
                        result = "0";
                    }
                } else {
                    Toast.makeText(FoodListActivity.this, "操作失败", Toast.LENGTH_SHORT).show();
                }
                break;
        }

    }

    @Override
    public void onFail() {
        showToast("网络连接错误");
    }
}